package com.example;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wlai
 * Date: 12/26/12
 * Time: 6:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class NineSpaceActivity extends Activity {
    private final static int[][] space = {{4, 9, 2, 3, 5, 7, 8, 1, 6}, {8, 3, 4, 1, 5, 9, 6, 7, 2}, {6, 1, 8, 7, 5, 3, 2, 9, 4}, {2, 7, 6, 9, 5, 1, 4, 3, 8}};
    private final static int idArr[] = {R.id.num1, R.id.num2,
            R.id.num3, R.id.num4, R.id.num5,
            R.id.num6, R.id.num7, R.id.num8, R.id.num9};

    List<TextView> views = new ArrayList<TextView>();
    private int curIndex = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nine_space);
        for (int i = 0; i < 9; i++) {
            TextView t = (TextView) findViewById(idArr[i]);
            views.add(t);
        }


    }

    public void turnLeft(View view) {
        curIndex = curIndex == 0 ? 3 : curIndex - 1;
        int i = 0;
        for (TextView v : views) {
            v.setText(space[curIndex][i] + "");
            i++;
        }
    }

    public void turnRight(View view) {
        /*curIndex = (curIndex == 3 ? 0 : curIndex + 1);
        int i = 0;
        for (TextView v : views) {
            v.setText(space[curIndex][i]+"");
            i++;
        }*/
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 1);
        /* 建立Intent和PendingIntent，来调用目标组件 */
        Intent intent = new Intent(NineSpaceActivity.this,
                AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent
                .getBroadcast(NineSpaceActivity.this, 0,
                        intent, 0);
        AlarmManager am;
                                /* 获取闹钟管理的实例 */
        am = (AlarmManager) getSystemService(ALARM_SERVICE);
                                /* 设置闹钟 */
        am.set(AlarmManager.RTC_WAKEUP, calendar
                .getTimeInMillis(), pendingIntent);
                                /* 设置周期闹 */
       /* am.setRepeating(AlarmManager.RTC_WAKEUP, System
                .currentTimeMillis()
                + (10 * 1000), (24 * 60 * 60 * 1000),
                pendingIntent);*/
    }
}